package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"xialou_platform/app/errorx"
	"xialou_platform/services/agent/model"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CourseByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCourseByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CourseByIdLogic {
	return &CourseByIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CourseByIdLogic) CourseById(in *agentClient.IdBody) (*agentClient.Course, error) {
	course, err := l.svcCtx.CourseModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if err == model.ErrNotFound {
			return nil, errorx.NewDefaultError("该课程不存在。")
		}
		return nil, err
	}

	res := &agentClient.Course{}
	copier.Copy(&res, course)
	res.CreateTime = course.CreateTime.Unix()
	res.UpdateTime = course.UpdateTime.Unix()

	return res, nil
}
